/*
*===============DdmgCloudPlatform3 V2.0===============
* DdmgCloudPlatform3 V2.0
* Copyright@大大买钢2018,All Rights Reserved
* Author: yiChangBin(2055960489@qq.com)
* Date: 2019/6/6  9:14
* TODO: 销售-->现货销售页面
*===============DdmgCloudPlatform3 V2.0===============
*/
<template>
    <div>
        <d2-container
                v-if="!loading"
                v-loading="!loading"
                element-loading-text="努力加载中"
                element-loading-spinner="el-icon-loading"
        />
        <sales-contract-look
                :type="type"
                key="look"
                v-if="(type==0 || type==3) && loading"
        />
        <sales-contract-edit
                :title-status="type==1?'新增':'编辑'"
                ref="edit"
                key="edit"
                :type="type"
                v-if="(type==1 || type==2) && loading"
        />
    </div>
</template>

<script>
  import { createNamespacedHelpers } from "vuex";
  import salesContractLook from "./components/salesContractLook";
  import salesContractEdit from "./components/salesContractEdit";
  import GetFromDbMixin from "@/mixin/GetFromDbMixin";

  const { mapActions } = createNamespacedHelpers("ddmg/salesContractOperation");

  export default {
    name: "salesContractOperation",
    mixins: [GetFromDbMixin],
    components: {
      salesContractLook,
      salesContractEdit,
    },

    data() {
      return {
        type: -1,
        loading: false,
      };
    },

    mounted() {
      this.type = this.$route.query.type;
      const order_no = this.$route.query.order_no;
      this.getInitInfo({ type: this.type, order_no });
    },

    // 第一次进入或从其他组件对应路由进入时触发
    beforeRouteEnter(to, from, next) {
      next((vm) => {
        vm.getStoreParams("salesContractOperation", to.params);
      });
    },

    methods: {
      ...mapActions(["getPageInitInfo"]),

      getStoreParams(fullPath, params) {
        this.getParamsFromDb(fullPath).then((res) => {
          const { type, order_no } = res;
          if (!params.isTabClick) {
            this.getInitInfo({ type, order_no });
          }
        });
      },

      getInitInfo({ type, order_no }) {
        this.loading = false;
        this.type = type;
        this.getPageInitInfo({ type, order_no })
        .then(() => {
          this.loading = true;
          this.$nextTick(() => {
            if (this.$refs["edit"]) this.$refs["edit"].resetFields();
          });
        })
        .catch((e) => {
          this.loading = false;
          this.$store.dispatch("d2admin/page/close", {
            tagName: "/contractManage/salesContractOperation",
            vm: this,
          });
        });
      },
    },
  };
</script>